<?php session_start(); ?>
<!DOCTYPE html>
<html>
<head>
  <link rel="stylesheet" type="text/css" href="css/projectstyle.css">

	<?php 
	if ($_SERVER["REQUEST_METHOD"] == "POST")
	{  	
		$condb = mysqli_connect("localhost","root","password","PROJECT");
		
		if(isset($_POST['courseId']) and isset($_POST['courseName']) and isset($_POST['startDate']) and isset($_POST['stopDate'])and isset($_SESSION['id']))
		{
		    $courseid= $_POST['courseId']; // sets variables to insert into database
		    $coursename= $_POST['courseName'];
		    $startdate = $_POST['startDate'];
		    $stopdate= $POST['stopDate'];
                    
                    
		    $id = $_SESSION['id'];// this variable is used to relate to specific user
                    
                    echo $id;

		    if(mysqli_connect_errno()) 
     		    {
      		    echo "connection failed " . mysqli_connect_error();
     		    }
                
		     $insert_1= "insert into COURSE (COURSE_ID, COURSE_NAME, START_DATE, STOP_DATE ) VALUES ('$courseid','$coursename','$startdate','$stopdate');"; //
		
		    if (!mysqli_query($condb,$insert_1))
 		    {
  		    die('Error: NO COURSE ' . mysqli_error($condb));
  		    }
		
		    $insert_2 ="INSERT INTO PERSON_TAKES_COURSE (COURSE_ID,ID) VALUES ('$courseid','$id');";
		    if (!mysqli_query($condb,$insert_2))
 		    {
  		    die('Error: ' . mysqli_error($con));
  		    }
	       } 
                   
                   
               
		
		
	

	       if(isset($_POST['kurser']))

		{
                 $COURSEID=$_POST['kurser'];
		 $delete = "delete from PERSON_TAKES_COURSE  WHERE COURSE_ID= '$COURSEID'";

		if (!mysqli_query($condb,$delete))
 		 {
  		  die('Error: ' . mysqli_error($condb));
  		 }

              } 
	}
	?>
   <title></title>
   <meta charset="UTF-8">
 </head>
  <body> 
       <div class="header2" id="drop">
	  Här väljer du kurs för borttagning.<?php 

          $id=$_SESSION['id'];// sets variable to session username
          if (isset($_SESSION['id']))
          { echo "<br/> id is set to ".$id;}
              
	  $condb = mysqli_connect("localhost","root","password","PROJECT");// creates connection to database
        

	  if(mysqli_connect_errno()) //checks connection and output error if connection fails
     		{
      		echo "connection failed " . mysqli_connect_error();
     		}
                //mysqli_query($condb,"SELECT COURSE.COURSE_ID,COURSE_NAME FROM COURSE;");
		$result = mysqli_query($condb,"SELECT COURSE.COURSE_ID,COURSE_NAME FROM PERSON_TAKES_COURSE, USER_INFO, COURSE WHERE PERSON_TAKES_COURSE.ID=USER_INFO.ID AND COURSE.COURSE_ID=PERSON_TAKES_COURSE.COURSE_ID AND  PERSON_TAKES_COURSE.ID='$id';");// gets the courses related to a specific user;

		echo "<form action=\"addcourse.php\" method=\"post\"name=\"kurser\">";
                echo  "<select name=\"kurser\">";
		echo  "<option value=\"\">Välj kurs</option>";

	  	while($row = mysqli_fetch_array($result))
         	{  
		$course_id=$row['COURSE_ID'];
		echo "<option value=\"".$course_id."\">".$row['COURSE_ID']." ".$row['COURSE_NAME']."</option>";
         	}

	    	echo "</select><br/>";
	    	echo "<input type=\"submit\" value=\"Ta bort kurs\">";
	    	echo "</form><br/>";
	?>
   </div>

   <form id ="frmc" name ="frmc" action="addcourse.php" method="post"><!--form used to add course-->

   <p id="instructions" class="instructions">Fyll i formuläret och spara för att lägga till kurs, observera att alla poster måste vara i fyllda.</p>
   <p>Kurskod: <input type="text" name ="courseId" required></input></p>
   <p>Kursnamn: <input type="text" name = "courseName" required></input></p>

  <p>Startdatum: <input type="text"name = "startDate" required></input></p>
 
 <p>Slutdatum: <input type="text"name = "stopDate" required></input></p>
   <input type="submit" value="spara">
 </form>

  </body>

